﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL1;
using dto2; //cần Add reference
using System.Data;
using System.Data.OleDb;

namespace BLL
{
    class MonhocDAL
    {
        /* Có thể lấy tất cả dữ liệu của một bảng như sau
public DataTable GetAll()
{
    DataProvider dataprovider = new DataProvider();
    return dataprovider.ExecuteSelectQuery("Select * from KHOA", null);
}
*/
        public List<MONHOC> GetAll()
        {

            MONHOC mh;
            List<MONHOC> list = new List<MONHOC>();
            DataProvider dataprovider = new DataProvider();
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from MONHOC", null);
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                mh = new MONHOC();
                mh.Mamon = Convert.ToByte(datatable.Rows[i]["Mamon"]);
                mh.Tenmon = datatable.Rows[i]["Tenmon"].ToString();
                list.Add(mh);
            }
            return list;
        }

        public MONHOC GetMONHOC(byte mamon)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.TinyInt);
            parameters[0].Value = mamon;
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from MONHOC where Mamon = @mamon", parameters);
            MONHOC mh = new MONHOC();
            if (datatable.Rows.Count != 0)
            {
                mh.Mamon = Convert.ToByte(datatable.Rows[0]["Mamon"]);
                mh.Tenmon = datatable.Rows[0]["Tenmon"].ToString();
                return mh;
            }
            else
                return null;
        }

        public bool UpdateMONHOC(byte mamon, string tenmon, byte monhoccu)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[3];
            parameters[0] = new OleDbParameter("@mamon", OleDbType.TinyInt);
            parameters[0].Value = mamon;
            parameters[1] = new OleDbParameter("@tenmon", OleDbType.Char);
            parameters[1].Value = tenmon;
            parameters[2] = new OleDbParameter("@monhoccu", OleDbType.TinyInt);
            parameters[2].Value = monhoccu;
            return dataprovider.ExecuteNonSelectQuery("update MONHOC set Mamon=@mamon, Tenmon=@tenmon where Mamon = @monhoccu", parameters);
        }
    }
}
